$(function(){

    //门店版图
    var storeLayout ={
    };

    /**
     * 初始化获取首页轮播图信息
     * **/
    storeLayout.initBanner = function(){
        $.ajax({
            async: false,
            url: '/banner/findBannerLayoutList',
            success: function (result) {
                if (result && result.code == 200) {
                    var slideWarp = $('#banner-layout');
                    var slideWidth = slideWarp.outerWidth();//获取轮播图宽度
                    var scale = (slideWidth/948).toFixed(2);
                    var slideHeight = 500*scale;
                    var html = '<ul>';
                    $.each(result.data,function(index,banner){
                        if( index <6 ){//最多取四张
                            if(banner.bannerUrl.indexOf("image") != -1 ){
                                html +='<li class="slide home-slide">';
                                html +='<img src="'+banner.bannerUrl+'" alt="" class="sliderimg" ></li>';
                            }
                        }
                    })
                    html +='</ul>';
                    slideWarp.empty().append(html);

                    //获取图片高度
                    //morease.resize();
                    /*$('#bannerDiv').width(slideWidth).height(slideHeight);*/
                    $('#banner-layout').unslider({
                        dots: true,
                        fluid: true
                    });

                    $('#banner-layout img:first').load(function(){
                        var bannerHeight = $('#banner-layout ul li').outerHeight();
                        $('#banner-layout').css("height",bannerHeight);
                        $('#banner-layout').css("min-height",bannerHeight);

                        //设置宽度一致
                        $('.addrDiv').css("width",$('#banner-layout').outerWidth());
                        //设置图片地区高度保持一直
                        $(".map-content").css('height',$('.store-contents').outerHeight());
                    });
                }
            },
            error: function () {
                layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
            }
        });
    }

    /**
     * 初始化
     * **/
    storeLayout.initStore = function(proName,cityName,name){

        var params = {};
        if( proName != undefined )
            params['proName'] = proName;
        if( cityName != undefined )
            params['cityName'] = cityName;
        if( name != undefined )
            params['name'] = name;


        $.ajax({
            async: false,
            url: '/store/findStoreList',
            data:params,
            success: function (result) {
                if (result && result.code == 200) {
                    var $newsHtml =  $('.store-contents .addrDiv');
                    $newsHtml.empty();
                    if (result.data != '') {
                        var $itemDiv ='';
                        if( result.data != null ){
                            $.each( result.data,function(index,store){
                                $itemDiv +='<a class="store-addr-item" name="'+store.addr+'" >';
                                $itemDiv +='<span style="font-size: 16px" title="'+store.name+'">'+store.name+'</span>';
                                $itemDiv +='<span style="font-size: 14px" title="'+store.tel+'"> 电话:'+store.tel+'</span>';
                                $itemDiv +='<span style="font-size: 14px" title="'+store.addr+'"> 地址:'+store.addr+'</span></a>';
                            })
                        }

                        $newsHtml.html($itemDiv);
                        //选择地址事件
                        $('.store-addr-item').on('click',function(){
                            $('.store-addr-item').removeClass("store-addr-item-select");
                            $(this).addClass("store-addr-item-select") ;

                            var mapCity = $(this)[0].name;
                            if( mapCity != undefined ){
                                setPlace(mapCity);
                            }

                        });
                        //默认选中第一个
                        $('.store-addr-item:first').click();
                    }
                }
            },
            error: function () {
                layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
            }
        });
    }

    /***
     * 初始化省份城市
     */
    storeLayout.initProvince = function(proId,cityId,proName,cityName){

        var $cityCode = $(cityId),$provinceCode = $(proId);
        //加载省份下拉框选项
        $provinceCode.empty().append('<option value="">请选择</option>');
        for (var i in cities) {
            if( i == proName ){
                $provinceCode.append('<option value="'+i+'" selected>' + i + '</option>');
                //构建城市select
                var s = cities[proName];
                $cityCode.html('<option value="">请选择</option>');
                for (var c in s) {
                    if( c == cityName ){
                        $cityCode.append('<option value="'+c+'" selected>' + c + '</option>');
                    }else{
                        $cityCode.append('<option value="'+c+'">' + c + '</option>');
                    }
                }
            }else{
                $provinceCode.append('<option value="'+i+'">' + i + '</option>');
            }
        }

        //根据省份下拉框值，加载相应的城市下拉框选项，并定位地图中心点
        $provinceCode.on('change',function () {
            var selectedVal = $(this).find(':selected').val();
            var s = cities[selectedVal];
            $cityCode.html('<option value="">请选择</option>');
            for (var i in s) {
                $cityCode.append('<option value="'+i+'">' + i + '</option>');
            }
        });

    }



    //搜索事件
    $('#searchBtn').on('click',function(){

        var provName = $('#provName').find(':selected').val();
        var cityName = $('#cityName').find(':selected').val();
        var name = $('#name').val();
        storeLayout.initStore(provName,cityName,name);
        var point = new BMap.Point(114.064552,22.548457);
        map.centerAndZoom(point,12);
        map.clearOverlays();    //清除地图上所有覆盖物
        map.addOverlay(new BMap.Marker(point));    //添加标注
    });

    //地图定位
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(114.064552,22.548457);
    map.centerAndZoom(point,12);
    map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
    map.addOverlay(new BMap.Marker(point));    //添加标注
    var localSearch = new BMap.LocalSearch(map);
    localSearch.enableAutoViewport(); //允许自动调节窗体大小
    function setPlace(myValue) {
        map.clearOverlays();    //清除地图上所有覆盖物
        localSearch.setSearchCompleteCallback(function (searchResult) {
            var poi = searchResult.getPoi(0);
            if( poi !=undefined ){
                map.centerAndZoom(poi.point, 16);
                var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地址对应的经纬度
                map.addOverlay(marker);
            }else{
                localSearch.search(myValue.replace("梦依诗专柜",''));//根据标题获取
            }
        });
        localSearch.search(myValue);
    }

    storeLayout.initProvince('#provName','#cityName');
    storeLayout.initBanner();
    //初始化
    storeLayout.initStore();
    header.selLiCss("hz");
    //面包屑名称
    $("#menu-ico span").html("•&nbsp;&nbsp;企业合作&nbsp;&nbsp;•&nbsp;&nbsp;门店版图");
});